#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<strings.h>
#include<string.h>
#include<sys/types.h>
#include<signal.h>
#include<sys/fcntl.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<sys/wait.h>
#include"tools.h"
#include"msgqueue.h"
#include"struct.h"
#include"get_keyboard.h"

static int msgid_ctos;
static int msgid_stoc;

static pid_t sub_pid[9];

void sigint(int signum)
{
	msgctl(msgid_ctos,IPC_RMID,NULL);
	msgctl(msgid_stoc,IPC_RMID,NULL);
	for(int i=0;i<9;i++)
	{
		kill(sub_pid[i],SIGKILL);
	}
	
}

int main()
{
	signal(SIGINT,sigint);

	init_id(ID_PATH,2018070000);

	msgid_ctos=get_ctos_msg();
	msgid_stoc=get_stoc_msg();

	sub_pid[0]=vfork();
	if(0==sub_pid[0])
	{
		execl("open","open",NULL);
	}

	sub_pid[1]=vfork();
	if(0==sub_pid[1])
	{
		execl("destroy","destroy",NULL);
	}

	sub_pid[2]=vfork();
	if(0==sub_pid[2])
	{
		execl("login","login",NULL);
	}

	sub_pid[3]=vfork();
	if(0==sub_pid[3])
	{
		execl("unlock","unlock",NULL);
	}

	sub_pid[4]=vfork();
	if(0==sub_pid[4])
	{
		execl("look","look",NULL);
	}

	sub_pid[5]=vfork();
	if(0==sub_pid[5])
	{
		execl("save","save",NULL);
	}

	sub_pid[6]=vfork();
	if(0==sub_pid[6])
	{
		execl("take","take",NULL);
	}

	sub_pid[7]=vfork();
	if(0==sub_pid[7])
	{
		execl("transf","transf",NULL);
	}
	
	sub_pid[8]=vfork();
	if(0==sub_pid[8])
	{
		execl("amend","amend",NULL);
	}

	wait(NULL);

}
